package com;

import com.entity.Order;
import com.entity.Stock;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vo.MergeVO;
import com.vo.StockVO;
import com.vo.req.MergeRequest;
import com.vo.req.OrderRequest;
import com.vo.req.Request;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

public class test {

    public static String url = "http://localhost:8080";
    static HttpHeaders requestHeaders = new HttpHeaders();
    static{
        requestHeaders.set("Accept-Charset", "utf-8");
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        requestHeaders.setContentType(type);
        requestHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
    }

    @Test
    public void consumer() throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();
        OrderRequest request = new OrderRequest();
        List<Request> requests = new ArrayList<>();
        requests.add(initRequest("1001","苹果手机",1));
        requests.add(initRequest("1002","华为手机",2));
        requests.add(initRequest("1003","小米手机",1));
        requests.add(initRequest("1004","天音手机",3));
        requests.add(initRequest("1002","华为手机",1));
        request.setOrderId("2021-01-20-1001");
        request.setOrder(requests);
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(request);
        HttpEntity<String> requestEntity = new HttpEntity<String>(s, requestHeaders);
        String result = restTemplate.postForObject(url + "/test/save", requestEntity, String.class);
        System.out.println(result);
    }

    public Request initRequest(String goodsId,String name,int number){
        Request request = new Request();
        request.setGoodsId(goodsId);
        request.setName(name);
        request.setNumber(number);
        return request;
    }

    @Test
    public void query(){
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<StockVO> response = restTemplate.getForEntity(url + "/test/query/stock/1001", StockVO.class);
        System.out.println(response.getBody());
    }

    @Test
    public void merge() throws JsonProcessingException {
        List<Stock> stocks = new ArrayList<>();
        stocks.add(initStock("1001","苹果手机",2));
        stocks.add(initStock("1002","华为手机",5));
        stocks.add(initStock("1003","小米手机",3));
        stocks.add(initStock("1004","天音手机",12));
        List<Order> orders = new ArrayList<>();
        orders.add(initOrder("1001","苹果手机",1));
        orders.add(initOrder("1002","华为手机",2));
        orders.add(initOrder("1003","小米手机",1));
        orders.add(initOrder("1004","天音手机",3));
        orders.add(initOrder("1002","华为手机",1));
        MergeRequest mergeRequest= new MergeRequest();
        mergeRequest.setOrder(orders);
        mergeRequest.setStock(stocks);
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(mergeRequest);
        HttpEntity<String> requestEntity = new HttpEntity<String>(s, requestHeaders);
        RestTemplate restTemplate = new RestTemplate();
        MergeVO mergeVO = restTemplate.postForObject(url + "/test/merge", requestEntity, MergeVO.class);
        System.out.println(mergeVO);
    }

    public Order initOrder(String goodsId,String name,int number){
        Order order = new Order();
        order.setGoodsId(goodsId);
        order.setName(name);
        order.setNumber(number);
        return order;
    }

    public Stock initStock(String goodsId, String name, int number){
        Stock stock = new Stock();
        stock.setGoodsId(goodsId);
        stock.setName(name);
        stock.setNumber(number);
        return stock;
    }
}
